/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.command.component.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.cdp.cdf.component.manager.serializer.CompOperationSerializer;
import com.inspur.edp.cdp.cdf.component.manager.serializer.CompParameterSerializer;
import com.inspur.edp.component.schema.GspCompOperation;
import com.inspur.edp.web.command.component.ComponentConstant;
import com.inspur.edp.web.command.component.metadata.CommandItem;
import com.inspur.edp.web.command.component.metadata.WebCommand;

import java.util.List;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class WebCommandSerializer extends CompOperationSerializer {

    protected void readExtendInfo(GspCompOperation method, JsonNode jsonNode) {
        WebCommand webCommand = (WebCommand) method;
        this.readCommandItems(webCommand, jsonNode);
    }

    private void readCommandItems(WebCommand webCommand, JsonNode commandNode) {
        ArrayNode itemsArrayNode = JsonUtil.getPropValue(commandNode, ComponentConstant.WebCommandField.ITEMS, ArrayNode.class);
        if (itemsArrayNode == null || itemsArrayNode.isEmpty()) {
            return;
        }
        List<CommandItem> commandItems = webCommand.getItems();
        for (JsonNode itemNode : itemsArrayNode) {
            CommandItem item = CommandItemSerializer.deserialize(itemNode);
            commandItems.add(item);
        }
    }

    protected void writeExtendInfo(GspCompOperation method, JsonNode jsonNode) {
        WebCommand webCommand = (WebCommand) method;
        this.writeCommandItems(webCommand, jsonNode);
    }

    private void writeCommandItems(WebCommand webCommand, JsonNode commandNode) {
        List<CommandItem> commandItems = webCommand.getItems();
        if (commandItems == null || commandItems.isEmpty()) {
            return;
        }
        ArrayNode arrayNode = JsonUtil.addEmptyArray(commandNode, ComponentConstant.WebCommandField.ITEMS);
        for (CommandItem item : commandItems) {
            JsonNode itemNode = CommandItemSerializer.serialize(item);
            arrayNode.add(itemNode);
        }
    }

    protected CompParameterSerializer getParameterSerializer() {
        return new ParameterSerializer();
    }

    protected GspCompOperation createGspCompMethod() {
        return new WebCommand();
    }
}
